import os
import random
from typing import Union

from PIL import Image, ImageDraw, ImageFont
import json
import re
import yaml
import numpy as np
from utils import text_util


# 圣遗物类
class Relic:
    relic_types = ["生之花", "死之羽", "时之沙", "空之杯", "理之冠"]  # 圣遗物类别列表
    main_entries = ["攻击力", "防御力", "生命值", "攻击力百分比", "生命值百分比", "防御力百分比",
                    "暴击率", "暴击伤害", "元素精通", "元素充能效率", "治疗加成",
                    "物理伤害加成", "火元素伤害加成", "水元素伤害加成", "草元素伤害加成",
                    "雷元素伤害加成", "风元素伤害加成", "冰元素伤害加成", "岩元素伤害加成"]  # 主词条列表

    main_entries0 = ["生命值"]  # 花主词条列表
    main_entries1 = ["攻击力"]  # 羽主词条列表
    main_entries2 = ["攻击力百分比", "生命值百分比", "防御力百分比", "元素精通", "元素充能效率"]  # 沙主词条列表
    main_entries3 = ["火元素伤害加成", "水元素伤害加成", "草元素伤害加成",
                     "雷元素伤害加成", "风元素伤害加成", "冰元素伤害加成", "岩元素伤害加成",
                     "物理伤害加成", "攻击力百分比", "生命值百分比", "防御力百分比", "元素精通"]  # 杯主词条列表
    main_entries4 = ["暴击率", "暴击伤害", "元素精通", "攻击力百分比", "生命值百分比", "防御力百分比",
                     "治疗加成"]  # 头主词条列表

    main_entries_n = [main_entries0,
                      main_entries1,
                      main_entries2,
                      main_entries3,
                      main_entries4]  # 按圣遗物类型顺序排列的主词条列表的列表

    percentage_entries = ["攻击力百分比", "生命值百分比", "防御力百分比", "元素充能效率",
                          "暴击率", "暴击伤害", "治疗加成",
                          "火元素伤害加成", "水元素伤害加成", "草元素伤害加成", "雷元素伤害加成",
                          "风元素伤害加成", "冰元素伤害加成", "岩元素伤害加成", "物理伤害加成", ]  # 百分比型词条

    secondary_lemmas = ["攻击力", "防御力", "生命值", "攻击力百分比", "防御力百分比", "生命值百分比", "暴击率",
                        "暴击伤害", "元素精通", "元素充能效率"]  # 副词条列表

    # yaml_load = yaml.safe_load(open("train.yaml", "r", encoding="utf-8").read())
    # names = yaml_load["names"]

    def __init__(self, img: Image = None, name: str = None, coordinate_name: str = None, 两件套: str = None,
                 四件套: str = None, relic_type: str = None):
        self.relic_type: str = relic_type  # 圣遗物类别
        self.name: str = name  # 圣遗物名字
        self.coordinate_name: str = coordinate_name  # 圣遗物套装名字
        self.img: Image.Image = img  # 圣遗物图片

        self.两件套: str = 两件套
        self.四件套: str = 四件套

        self.main_entry_type: str = None  # 主词条类别
        self.main_entry_number: Union[float, int] = None  # 主词条数值

        self.secondary_lemma_type: list[str] = []  # 副词条
        self.secondary_lemma_number: list[Union[float, int]] = []  # 副词条数值

        self.locked = False  # 圣遗物是否加锁
        self.level = 0  # 圣遗物等级

    def tostring(self):
        return str(self.relic_type) + " " + "主词条:" + str(self.main_entry_str) \
            + " " + "副词条:" + str(self.secondary_lemma_str) + "上锁" + str(self.locked)

    def tostring1(self):
        return str(self.relic_type) + " " + "主词条:" + str(self.main_entry_type) \
            + " " + "副词条:" + str(self.secondary_lemma_type) + "上锁" + str(self.locked) + self.coordinate_name

    # 词条的类别不会出现百分比字样，只会在后面加百分号表示是百分比
    @property
    def main_entry_str(self):
        if self.main_entry_type in Relic.percentage_entries:
            return self.main_entry_type.replace("百分比", "") + "+" + str(self.main_entry_number) + "%"
        else:
            return self.main_entry_type + "+" + str(self.main_entry_number)

    @property
    def secondary_lemma_str(self) -> list[str]:
        secondary_lemma_str = []
        i: str
        for i in range(len(self.secondary_lemma_type)):
            if self.secondary_lemma_type[i] in Relic.percentage_entries:
                secondary_lemma_str.append(
                    self.secondary_lemma_type[i].replace("百分比", "") + "+" + str(
                        self.secondary_lemma_number[i]) + "%")
            else:
                secondary_lemma_str.append(self.secondary_lemma_type[i] + "+" + str(self.secondary_lemma_number[i]))
        return secondary_lemma_str

#
# # 使用这个函数创建圣遗物列表
# def get_relics(src: str) -> list:  # "images/圣遗物"
#     relic_list = []
#     for coordinate_name in os.listdir(src):
#         两件套 = "2件套："
#         四件套 = "4件套："
#         for s in open(src + "/" + coordinate_name + "/两件套.txt", encoding="utf-8").readlines():
#             两件套 += s
#
#         for s in open(src + "/" + coordinate_name + "/四件套.txt", encoding="utf-8").readlines():
#             四件套 += s
#
#         for z in os.listdir(src + "/" + coordinate_name):
#             if z.endswith("png"):
#                 print(z)
#                 # (self, img: Image, name: str, coordinate_name: str, 两件套: str, 四件套: str, relic_type: str):
#                 relic_list.append(Relic(img=Image.open(src + "/" + coordinate_name + "/" + z) \
#                                         , name=z[4:].replace(".png", ""), relic_type=z[0:3] \
#                                         , coordinate_name=coordinate_name, 两件套=两件套, 四件套=四件套))
#     return relic_list
#
#
# if __name__ == '__main__':
#     relic_list = get_relics(src="images/圣遗物")
#     counter = 0
#     # train_y = {}
#     # print(train_y)
#
#     for i in range(6):
#         for relic in relic_list:
#             relic.random_entry()
#             train_x, train_y = relic.train_data
#             train_x.save("datasets/images/train/" + str(counter) + ".png")
#             # train_y[(100 * i) + relic_list.index(relic)] = list(relic.train_y)
#             with open('datasets/labels/train/' + str(counter) + ".txt", 'w', encoding="utf-8") as f:
#                 for s in train_y:
#                     f.write(s + "\n")
#             print(str(i) + "+" + str(relic_list.index(relic)))
#             counter += 1
#     # with open('train_y.json', 'w', encoding="utf-8") as f:
#     #     json.dump(train_y, f)
#
#     # pass
